package mws;



import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 单个线程池的包装实现类
 * 直接持有线程池实例，实现ExecutorService接口，便于后续扩展
 */
public class OwenThreadPool implements ExecutorService {

    // 被包装的线程池实例
    protected final ExecutorService delegate;
    // 关闭状态标记
    protected final AtomicBoolean isShutdown = new AtomicBoolean(false);

    /**
     * 构造函数：直接传入线程池实例
     * @param delegate 被包装的线程池
     */
    public OwenThreadPool(ExecutorService delegate) {
        if (delegate == null) {
            throw new IllegalArgumentException("线程池实例不能为空");
        }
        this.delegate = delegate;
    }

    /**
     * 获取被包装的线程池实例
     * @return 线程池实例
     */
    public ExecutorService getDelegate() {
        return delegate;
    }

    // ------------------------------ 实现ExecutorService接口方法 ------------------------------

    @Override
    public void execute(Runnable command) {
        checkShutdown();
        delegate.execute(command);
    }

    @Override
    public Future<?> submit(Runnable task) {
        checkShutdown();
        return delegate.submit(task);
    }

    @Override
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) {
      return null;
    }

//  方法 不会覆盖或实现超类型的方法  666  也就是 default的方法不能修改 ???

    //
    public void close() {
       throw new RuntimeException("原因未知 ");
    }

    @Override
    public <T> Future<T> submit(Runnable task, T result) {
        checkShutdown();
        return delegate.submit(task, result);
    }

    @Override
    public <T> Future<T> submit(Callable<T> task) {
        checkShutdown();
        return delegate.submit(task);
    }

    @Override
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
        checkShutdown();
        return delegate.invokeAll(tasks);
    }

    @Override
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
        checkShutdown();
        return delegate.invokeAll(tasks, timeout, unit);
    }

    @Override
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
        checkShutdown();
        return delegate.invokeAny(tasks);
    }

    @Override
    public void shutdown() {
        if (isShutdown.compareAndSet(false, true)) {
            delegate.shutdown();
        }
    }

    @Override
    public List<Runnable> shutdownNow() {
        if (isShutdown.compareAndSet(false, true)) {
            return delegate.shutdownNow();
        }
        return List.of();
    }

    @Override
    public boolean isShutdown() {
        return isShutdown.get();
    }

    @Override
    public boolean isTerminated() {
        return delegate.isTerminated();
    }

    @Override
    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
        return delegate.awaitTermination(timeout, unit);
    }

    /**
     * 检查线程池是否已关闭
     */
    protected void checkShutdown() {
        if (isShutdown.get()) {
            throw new RejectedExecutionException("线程池已关闭，无法提交任务");
        }
    }
}
    